package cpre556.project2.eclipsePlugin.support;

import java.io.*;
import java.util.Vector;

/**
 * Wrapper for RATS
 */
public class RATStoXML extends Plugin{
	private BufferedReader reader = null;
	
	/**
	 * Plug-in name
	 */
	protected String name = "rats";
	
	/**
	 * Constructor
	 * @param s
	 */
	public RATStoXML(){
		super();
		
		String location = settings.getPlugin(name);
		
		if(location.equals("")){
			settings.saveSetting(name, saver.getExe());
		}
	}
	
	/**
	 * Parse the output file from rats
	 */
	protected void parseFile(){
		try{
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(saver.getTxt())));
			
			vulnerabilityList = new Vector<Vulnerability>();
			Vulnerability tempVulnerability;
			Vector<Vulnerability> tempVulnerabilityList = new Vector<Vulnerability>();			
			
			String inLine = reader.readLine();
			inLine = reader.readLine();
			inLine = reader.readLine();
			
			while(!inLine.startsWith("Total lines analyzed:")){                                
								
				if(inLine.indexOf(":") != -1){
					// Set the filename, line number, priority and function name
					while(inLine.indexOf(":")!=-1){
						tempVulnerability = new Vulnerability();
						
						String f = inLine.substring(0, inLine.indexOf(".c:") + 2);
						inLine = inLine.substring(inLine.indexOf(".c:") + 3, inLine.length());
						
						String ln = inLine.substring(0,inLine.indexOf(":"));
						inLine = inLine.substring(inLine.indexOf(":") + 2, inLine.length());
											
						String p = inLine.substring(0,inLine.indexOf(":"));
						inLine = inLine.substring(inLine.indexOf(":") + 2, inLine.length());
						
						String fn = inLine.substring(inLine.lastIndexOf(":") + 1).trim();
						inLine = inLine.substring(inLine.indexOf(":") + 2, inLine.length());					
						
						tempVulnerability.setFilename(f);
						tempVulnerability.setLineNumber(Integer.valueOf(ln).intValue());
						tempVulnerability.setPriority(p);
						tempVulnerability.setFunctionName(fn.trim());
						tempVulnerabilityList.add(tempVulnerability);	
						
						inLine = reader.readLine();
					}
					
					String desc = inLine;
					inLine = reader.readLine();
					
					while(!inLine.equalsIgnoreCase("")){
						desc = desc + " " + inLine;
						inLine = reader.readLine();
					}
					
					String sugg = "None given.";
					
					int tempSize = tempVulnerabilityList.size();
					
					for(int i = 0; i < tempSize; i++){							
						tempVulnerabilityList.get(i).setDescription(desc);
						tempVulnerabilityList.get(i).setSuggestion(sugg);
						
						vulnerabilityList.add(tempVulnerabilityList.get(i));
					}								
					
					tempVulnerabilityList.clear();				
				}
				
				inLine = reader.readLine();
			}
			
			reader.close();
		}
		catch(FileNotFoundException fnfe){
			System.err.println("File not found:" + saver.getFile() + ".");			
		}
		catch(IOException ioe){
			System.err.println("I/O error:" + ioe);
		}
	}
}
